<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class ReportController extends AdminbaseController{
	function _initialize() {
		parent::_initialize();
	}

	public function index(){

		//查询所有的景区列表
		$Term_relationships = M('Term_relationships');
		$Posts              = M('Posts');
		$Order              = M('Order');
		//获取所有景区的id
		$post_id_array = $Term_relationships->where("status=1")->order('object_id desc')->getField('object_id',true);
		
		$post_id_str   = implode(',',$post_id_array);
		$list          = $Posts->where(array('id'=>array('IN',$post_id_str)))->field('id,post_title,post_date')->select();
	  
    if($_SESSION['ADMIN_ID']!=1){
      $where['o.admin_id'] = $_SESSION['ADMIN_ID'];
    }
		$where['p.id'] = array('IN',$post_id_str);
        $order_info    = $Order->alias('o')
                               ->join("__POSTS__ p on p.id = o.post_id","right")
                               ->where($where)
                               ->field('p.post_title,count(o.post_id) as number,sum(o.ticket_price) as total_price,p.adult_price')
                               ->group('p.id')
                               ->select();
        $result['number'] = $result['total_price'] = 0;
        foreach ($order_info as $key => $val) {
          $order_info[$key]['total_price'] = $order_info[$key]['total_price'] ? $order_info[$key]['total_price']:0;  
        	$scenic_list[]    = $val['post_title'];
        	$result['number']        += $val['number'];
        	$result['total_price']   += $val['total_price'];
        }
        $result['scenic_list'] = implode('、',$scenic_list);
      
    $this->assign('result',$result);
    $this->assign('order_info',$order_info);
		$this->assign('list',$list);
		
		$this->display();
	}
	//接口返回统计信息
	public function reportInfo(){
		    if(!empty($_POST['begin_time']) && !empty($_POST['end_time'])){
			    $where['print_time'] = array("BETWEEN","{$_POST['begin_time']},{$_POST['end_time']}");
		    }
        
        if(empty($_POST['begin_time']) && !empty($_POST['end_time'])){
        	$where['print_time'] = array("ELT",$_POST['end_time']);
        }

        if(!empty($_POST['begin_time']) && empty($_POST['end_time'])){
        	$where['print_time'] = array("EGT",$_POST['begin_time']);
        }

		    $Order              = M('Order');
		    $where_posts['id'] = array('IN',implode(',',$_POST['id']));
       
        $post_list     = M('Posts')->where($where_posts)->field('id,post_title')->select();
        
        if($_SESSION['ADMIN_ID']!=1){
            $where['admin_id'] = $_SESSION['ADMIN_ID'];
        }
        foreach ($post_list as $key => $val) {
            $where['post_id'] = $val['id'];
            $order_info[$key] = $Order->where($where)->field('count(post_id) as number,sum(ticket_price) as total_price')->find();
            $order_info[$key]['post_title']  = $val['post_title'];
            $order_info[$key]['total_price'] = $order_info[$key]['total_price'] ? $order_info[$key]['total_price']:0;
        }


       // $order_info    = $Order->alias('o')
       //                         ->join("__POSTS__ p on p.id = o.post_id","right")
       //                         ->where($where)
       //                         ->field('p.post_title,count(o.post_id) as number,sum(o.ticket_price) as total_price,p.adult_price')
       //                         ->group('p.id')
       //                         ->select();
       //  dump($order_info);die;
       //  $result['number'] = $result['total_price'] = 0;
       //  dump($order_info);
        foreach ($order_info as $key => $val) {
            $order_info[$key]['total_price'] = $order_info[$key]['total_price'] ? $order_info[$key]['total_price']:0;
          	$scenic_list[]    = $val['post_title'];
          	$result['number']        += $val['number'];
          	$result['total_price']   += $val['total_price'];
        }
        $result['total_price'] = round($result['total_price'],2);

        
        $result['scenic_list'] = implode('、',$scenic_list);
        $result['scenic_list'] = $result['scenic_list'] ? $result['scenic_list']:'---';
        $result['order_info']  = $order_info;   
		    $this->ajaxReturn($result);
	}
}